package com.heima.article.service.impl;

import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.IArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 文章列表查询
     * @param dto
     * @param type
     * @return
     */
    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto, Long type) {
        //1,参数校验
        //1.1 分页参数校验
        if(dto.getSize()!=null){
            dto.setSize(10);
        }
        //1.2类型参数检验
        if (!type.equals(1) &&!type.equals(2)){
            type=2L;
        }
        //1.3文章频道校验
        if (dto.getTag().isEmpty()){
            dto.setTag("__all__");
        }
        //1.4时间校验
        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }

        //2.调用mapper接口，查询数据
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, type);
        //返回数据
        return ResponseResult.okResult(apArticles);
    }
}
